package model.User;



import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import model.User.entity.User;
import model.User.service.IUserService;
import model.util.JwtUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.test.context.junit4.SpringRunner;


/**
 * Created by Sunshine、宸 on 2019/6/24.
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class testUserService {
    @Autowired
    private IUserService iUserService;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;


    @Test
    public void testSave(){
        User user=new User();
        user.setUserName("mock111");
        user.setUserEmail("11456454@qq.com");
        user.setUserPhone("12332111");
        user.setUserSex(0);
        user.setUserPassword("jack");
        user.setUserConfirmpwd("jack");

        iUserService.Save(user);
    }


    @Test
    public void testfindById(){
        log.info("查找成功！");
        System.out.println(iUserService.findById(1));
    }

    @Test
    public void testfindAll(){
        System.out.println(iUserService.findAll());
    }

    @Test
    public void testDelById(){
        iUserService.delById(6);
    }

    @Test
    public void testfindMoHu(){
        System.out.println(iUserService.findMoHu("k"));
    }

    @Test
    public void testUpdateUser(){
        User user =iUserService.findById(1);
        String pwd=bCryptPasswordEncoder.encode("admin");
        String password=bCryptPasswordEncoder.encode("admin");
        user.setUserConfirmpwd(password);
        user.setUserPassword(pwd);
        iUserService.updateByUser(user);
    }

    @Test
    public void testfindByPhone(){
        System.out.println(iUserService.finByPhone("18261687290"));
    }

    @Test
    public void testfindByEmail(){
        User user=new User();
        user.setUserEmail("1142351748@qq.com");
        System.out.println(iUserService.findByEmail(user));
    }


    @Test
    public void testToken(){
        String token=jwtUtil.createJWT("1","tom","admin");
        System.out.println(token);
    }

    @Test
    public void testParse(){
        Claims token=jwtUtil.parseJWT("eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxIiwic3ViIjoiMTgyNjE2ODcyOTAiLCJpYXQiOjE1NjIxMjQ3ODcsInJvbGVzIjoiYWRtaW4ifQ.JynjQ7dbYIzCSlNWmrro9gZ0y6ksgU018zP58xCJXDk");
        System.out.println(token.getSubject());
    }

    @Test
    public void testfindByUserName(){
        System.out.println(iUserService.findByUserName("tom"));
    }


}
